<template>
  <div class="zh-permission-settings">
    <nav-header>
      <template slot="menuBefore">
        <li>
          <router-link to="/user/user/platform-role" tag="a">平台角色管理</router-link>
        </li>
        <li class="gap">&gt;</li>
      </template>
      <li>{{roleTitle}}</li>
    </nav-header>

    <div v-loading="loading.status" :element-loading-text="loading.txt">
      <template v-for="(item,inx1) in nodeData">
        <div :key="inx1">
          <el-row class="header-title">
            <el-col :span="6" :class="{ activeTop: inx1 == 0 }">{{item.title}}</el-col>
            <el-col :span="18" :class="{ activeTop: inx1 == 0 }">功能</el-col>
          </el-row>

          <el-row v-for="(item2,inx2) in item.children" :key="item2.id" class="content-setting">
            <el-col :span="6">
              <el-checkbox
                :disabled="setValue(item2)"
                v-model="item2.selected"
                @change="selectNodeChange($event, item2.id, inx1, inx2)"
              >{{item2.title}}</el-checkbox>
            </el-col>
            <el-col :span="18">
              <el-checkbox
                :disabled="setValue(item2, item3)"
                v-for="(item3, inx3) in item2.children"
                v-model="item3.selected"
                @change="selectNodeChange($event, item3.id, inx1, inx2, inx3)"
                :label="item3.title"
                :key="item3.id"
              ></el-checkbox>
            </el-col>
          </el-row>
        </div>
      </template>
    </div>

    <div class="submit-button">
      <el-button @click="cancel()">取消</el-button>
      <el-button type="primary" plain @click="reset_confirm()">重置</el-button>
      <el-button type="primary" :disabled="subAbled" @click="submitRole()">提交</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      subAbled: false, // 控制提交按钮是否可点击
      loadingTotalTime: 0,
      roleTitle: "权限设置",
      nodeIds: [],
      nodeData: [],
      defaultNodeIds: [], // 默认选中的权限
      loading: {
        status: true,
        txt: "获取数据...",
      },
    };
  },
  created() {
    this.getRoleData();
    this.getDefaultSelectNode();
    this.getSelectNode();
    this.getRoleList();
  },
  watch: {
    loadingTotalTime: {
      handler(n) {
        if (n === 3) {
          this.loading.status = false;
        }
      },
    },
  },
  methods: {
    /**
     * 获取当前角色名称
     */
    getRoleList() {
      let success = (res) => {
        if (!is_empty(res.data)) {
          this.roleTitle = res.data[0].name;
        }
      };
      const params = {
        id: this.$route.params.id,
      };

      this.$ajax.get("api/role/role", { params, func: { success: success } });
    },
    /**
     * 两个数组是否存在相同的值
     * @param {Array} arr1 默认数组
     * @param {Array} arr2 要比较的数组
     */
    array2Array(arr1, arr2) {
      let len1 = arr1.length;
      let len2 = arr2.length;

      for (let i = 0; i < len1; i++) {
        for (let j = 0; j < len2; j++) {
          if (arr1[i] == arr2[j]) {
            return true;
          }
        }
      }

      return false;
    },
    /**
     * 树形数据判断，父子联动
     * @param {Boolean} selected 是否勾选
     * @param {Number} nodeid 当前id
     * @param {Number} inx1 一级菜单下标
     * @param {Number} inx2 二级菜单下标
     * @param {Number} inx3 三级菜单下标
     */
    selectNodeChange(selected, nodeid, inx1 = -1, inx2 = -1, inx3 = -1) {

      this.nodeData[inx1].selected = true;
      this.setNodes(selected, nodeid);
      this.setNodes(true, this.nodeData[inx1].id);
      let classId = [1034, 1035, 1036, 1037, 1038, 1039, 1040, 1100];

      if (inx3 === -1) {
        // 二级菜单
        if (selected) {
          this.nodeData[inx1].children[inx2].selected = true;
          let threeList = this.nodeData[inx1].children[inx2].children;

          if (threeList.length) {
            for (let j = threeList.length - 1; j >= 0; j--) {
              // 教师身份才有 教学中心--问答中心，应用-我的问答
              if (
                +this.$route.params.id !== 6 &&
                [1017, 1163].includes(threeList[j].id)
              ) {
                continue;
              }

              // 教师、管理员特有 app 权限：网络教研、学科圈组、协同备课、请假审批
              if (
                ![2, 3, 6].includes(+this.$route.params.id) &&
                [1180, 1181, 1182, 1189].includes(threeList[j].id)
              ) {
                continue;
              }

              // 家长、学生app特有权限：成长档案
              if (
                ![4, 5].includes(+this.$route.params.id) &&
                1186 === threeList[j].id
              ) {
                continue;
              }

              // 教师、家长、学生app特有权限：作业、作业分析、班级通知、班级课表、请假
              if (
                ![4, 5, 6].includes(+this.$route.params.id) &&
                [1184, 1185, 1187, 1188, 1190].includes(threeList[j].id)
              ) {
                continue;
              }

              // 只有教师可以勾选学生成绩、学生档案
              if (
                +this.$route.params.id !== 6 &&
                [1132, 1139].includes(threeList[j].id)
              ) {
                continue;
              }

              // 只有学生可以点击学籍管理
              if (
                +this.$route.params.id !== 4 &&
                [1150, 1151, 1154].includes(threeList[j].id)
              ) {
                continue;
              }

              // 学生和家长不可以勾选智慧组卷
              if (
                [4, 5].includes(+this.$route.params.id) &&
                threeList[j].id === 1055
              ) {
                continue;
              }

              if (
                [4, 5].includes(+this.$route.params.id) &&
                [1045, 1046, 1108, 1127].includes(threeList[j].id)
              ) {
                continue;
              }

              if (
                ![4, 5, 6].includes(+this.$route.params.id) &&
                [1107, 1109, 1131, 1133].includes(threeList[j].id)
              ) {
                continue;
              }

              if (
                +this.$route.params.id !== 4 &&
                [1029, 1030].includes(threeList[j].id)
              ) {
                continue;
              }

              // if (threeList[j].id === 1020) {
              //   if ([4, 5, 6].includes(+this.$route.params.id)) {
              //     if (!this.nodeIds.includes(1033)) {
              //       continue;
              //     }
              //   } else {
              //     continue;
              //   }
              // }

              this.setNodes(true, threeList[j].id);
              threeList[j].selected = true;
              this.setLinkage(true, inx1, inx2, j);
            }
          }
        } else {
          let notDefault = false;
          let parent_select2 = false;

          if (in_array(+this.nodeData[inx1].children[inx2].id, this.defaultNodeIds)) {
            this.nodeData[inx1].children[inx2].selected = true;
          } else {
            notDefault = true;
          }

          //提取id列作为一位数组
          let sub_nodeids2 = getOneColumn(this.nodeData[inx1].children, "id");
          let inter_arr2 = arrayIntersection(sub_nodeids2, this.nodeIds);
          let threeList = this.nodeData[inx1].children[inx2].children;

          parent_select2 = inter_arr2.length > 0 ? true : false;
          this.nodeData[inx1].selected = parent_select2;
          this.setNodes(parent_select2, this.nodeData[inx1].id);

          if (threeList.length) {
            for (let i = threeList.length - 1; i >= 0; i--) {
              if (
                +this.$route.params.id !== 6 &&
                [1132, 1139].includes(threeList[i].id)
              ) {
                continue;
              }

              // 教师、管理员特有 app 权限：网络教研、学科圈组、协同备课、请假审批
              if (
                ![2, 3, 6].includes(+this.$route.params.id) &&
                [1180, 1181, 1182, 1189].includes(threeList[i].id)
              ) {
                continue;
              }

              // 家长、学生app特有权限：成长档案
              if (
                ![4, 5].includes(+this.$route.params.id) &&
                1186 === threeList[i].id
              ) {
                continue;
              }

              // 教师、家长、学生app特有权限：作业、作业分析、班级通知、班级课表、请假
              if (
                ![4, 5, 6].includes(+this.$route.params.id) &&
                [1184, 1185, 1187, 1188, 1190].includes(threeList[i].id)
              ) {
                continue;
              }

              // 教师身份才有 教学中心--问答中心，应用-我的问答
              if (+this.$route.params.id !== 6 && [1017, 1163].includes(threeList[i].id)) {
                continue;
              }

              // 只有学生可以点击学籍管理
              if (
                +this.$route.params.id !== 4 &&
                [1150, 1151, 1154].includes(threeList[i].id)
              ) {
                continue;
              }

              // 学生和家长不可以勾选智慧组卷
              if (
                [4, 5].includes(+this.$route.params.id) &&
                threeList[i].id === 1055
              ) {
                continue;
              }

              if (
                [4, 5].includes(+this.$route.params.id) &&
                [1108, 1127].includes(threeList[i].id)
              ) {
                continue;
              }

              if (
                ![4, 5, 6].includes(+this.$route.params.id) &&
                [1107, 1109, 1131, 1133].includes(threeList[i].id)
              ) {
                continue;
              }

              if (
                +this.$route.params.id !== 4 &&
                [1029, 1030].includes(threeList[i].id)
              ) {
                continue;
              }

              // if (threeList[i].id === 1020) {
              //   if ([4, 5, 6].includes(+this.$route.params.id)) {
              //     if (!this.nodeIds.includes(1033)) {
              //       continue;
              //     }
              //   } else {
              //     continue;
              //   }
              // }

              this.setNodes(false, threeList[i].id);

              if (!in_array(parseInt(threeList[i].id), this.defaultNodeIds)) {
                threeList[i].selected = false;
              }

              this.setLinkage(false, inx1, inx2, i);
            }
          }
        }

        // 个人中--收藏
        if (
          inx1 === 0 &&
          inx2 == 4 &&
          this.nodeData[inx1].children[inx2].id === 1041
        ) {
          this.nodeData[1].children[0].children.forEach((val1, index1) => {
            if (val1.id === 1130) {
              this.setNodes(
                selected,
                this.nodeData[1].children[0].children[index1].id
              );
              this.nodeData[1].children[0].children[index1].selected = selected;
            }
          });
        }

        // 应用--收藏
        if (inx1 === 1 && inx2 == 0 && this.nodeData[inx1].children[inx2].id === 1047) {
          
          this.nodeData[0].children[4].selected = selected;
          this.setNodes(selected, this.nodeData[0].children[4].id);

          this.nodeData[0].children[4].children.forEach((val1, index1) => {
            if ([4, 5].includes(+this.$route.params.id)) {
              if (![1045, 1046].includes(val1.id)) {
                this.setNodes(
                  selected,
                  this.nodeData[0].children[4].children[index1].id
                );
                this.nodeData[0].children[4].children[
                  index1
                ].selected = selected;
              }
            } else {
              this.setNodes(
                selected,
                this.nodeData[0].children[4].children[index1].id
              );
              this.nodeData[0].children[4].children[index1].selected = selected;
            }
          });
        }

        if (+this.$route.params.id === 6 && inx1 === 0 &&inx2 == 3 && this.nodeData[inx1].children[inx2].id === 1033) {
          this.nodeData[1].children[0].children.forEach((val1, index1) => {
            if (val1.id === 1132) {
              this.setNodes(
                selected,
                this.nodeData[1].children[0].children[index1].id
              );
              this.nodeData[1].children[0].children[index1].selected = selected;
            }
          });
        }

        if (
          +this.$route.params.id === 6 &&
          inx1 === 1 &&
          inx2 == 0 &&
          this.nodeData[inx1].children[inx2].id === 1047
        ) {
          this.nodeData[0].children[3].children.forEach((val1, index1) => {
            if (val1.id === 1036) {
              this.setNodes(
                selected,
                this.nodeData[0].children[3].children[index1].id
              );
              this.nodeData[0].children[3].children[index1].selected = selected;
            }
          });

          if (!this.array2Array(this.nodeIds, classId)) {
            this.nodeData[0].children[3].selected = selected;
            this.setNodes(selected, this.nodeData[0].children[3].id);
          }
        }
      } else {
        // 三级菜单
        if (selected) {
          this.nodeData[inx1].children[inx2].selected = true;

          this.setNodes(selected, this.nodeData[inx1].children[inx2].id);
          this.setLinkage(selected, inx1, inx2, inx3);
        } else {
          let notDefault = false;
          let parent_select1 = false;
          let parent_select2 = false;

          if (in_array(+this.nodeData[inx1].children[inx2].children[inx3].id, this.defaultNodeIds)) {
            this.nodeData[inx1].children[inx2].children[inx3].selected = true;
          } else {
            notDefault = true;
            this.nodeData[inx1].children[inx2].children[inx3].selected = false;
          }

          //提取id列作为一位数组
          let sub_nodeids1 = getOneColumn(
            this.nodeData[inx1].children[inx2].children,
            "id"
          );
          let inter_arr1 = arrayIntersection(sub_nodeids1, this.nodeIds);

          parent_select1 = inter_arr1.length > 0 ? true : false;
          if (in_array(parseInt(this.nodeData[inx1].children[inx2].id), this.defaultNodeIds)
          ) {
            this.nodeData[inx1].children[inx2].selected = true;
          } else {
            this.nodeData[inx1].children[inx2].selected = parent_select1;
          }

          this.setNodes(parent_select1, this.nodeData[inx1].children[inx2].id);

          //提取id列作为一位数组
          let sub_nodeids2 = getOneColumn(this.nodeData[inx1].children, "id");
          let inter_arr2 = arrayIntersection(sub_nodeids2, this.nodeIds);

          parent_select2 = inter_arr2.length > 0 ? true : false;

          if (in_array(parseInt(this.nodeData[inx1].id), this.defaultNodeIds)) {
            this.nodeData[inx1].selected = true;
          } else {
            this.nodeData[inx1].selected = parent_select2;
          }

          this.setNodes(parent_select2, this.nodeData[inx1].id);

          notDefault && this.setLinkage(selected, inx1, inx2, inx3);
        }

        if (inx1 === 1 && inx2 === 0 && this.nodeData[inx1].children[inx2].children[inx3].id === 1130) {
          this.nodeData[0].children[4].selected = selected;
          this.setNodes(selected, this.nodeData[0].children[4].id);

          this.nodeData[0].children[4].children.forEach((val1, index1) => {
            if ([4, 5].includes(+this.$route.params.id)) {
              if (![1045, 1046].includes(val1.id)) {
                this.setNodes(
                  selected,
                  this.nodeData[0].children[4].children[index1].id
                );
                this.nodeData[0].children[4].children[
                  index1
                ].selected = selected;
              }
            } else {
              this.setNodes(
                selected,
                this.nodeData[0].children[4].children[index1].id
              );
              this.nodeData[0].children[4].children[index1].selected = selected;
            }
          });
        }

        if (selected && this.nodeData[inx1].children[inx2].id === 1041) {
          this.nodeData[1].children[0].children.forEach((val1, index1) => {
            if (val1.id === 1130) {
              this.setNodes(
                selected,
                this.nodeData[1].children[0].children[index1].id
              );
              this.nodeData[1].children[0].children[index1].selected = selected;
            }
          });
        }

        if (!selected && this.nodeData[inx1].children[inx2].id === 1041) {
          if (!this.nodeIds.includes(1041)) {
            this.nodeData[1].children[0].children.forEach((val1, index1) => {
              if (val1.id === 1130) {
                this.setNodes(
                  selected,
                  this.nodeData[1].children[0].children[index1].id
                );
                this.nodeData[1].children[0].children[
                  index1
                ].selected = selected;
              }
            });
          }
        }

        if (
          +this.$route.params.id === 6 &&
          inx1 === 1 &&
          inx2 === 0 &&
          this.nodeData[inx1].children[inx2].children[inx3].id === 1132
        ) {
          this.nodeData[0].children[3].selected = selected;
          this.setNodes(selected, this.nodeData[0].children[3].id);

          this.nodeData[0].children[3].children.forEach((val1, index1) => {
            if (val1.id === 1036) {
              this.setNodes(
                selected,
                this.nodeData[0].children[3].children[index1].id
              );
              this.nodeData[0].children[3].children[index1].selected = selected;
            }
          });
        }

        if (
          +this.$route.params.id === 6 &&
          selected &&
          this.nodeData[inx1].children[inx2].children[inx3].id === 1036
        ) {
          this.nodeData[1].children[0].children.forEach((val1, index1) => {
            if (val1.id === 1132) {
              this.setNodes(
                selected,
                this.nodeData[1].children[0].children[index1].id
              );
              this.nodeData[1].children[0].children[index1].selected = selected;
            }
          });
        }

        if (
          +this.$route.params.id === 6 &&
          !selected &&
          this.nodeData[inx1].children[inx2].children[inx3].id === 1036
        ) {
          this.nodeData[1].children[0].children.forEach((val1, index1) => {
            if (val1.id === 1132) {
              this.setNodes(
                selected,
                this.nodeData[1].children[0].children[index1].id
              );
              this.nodeData[1].children[0].children[index1].selected = selected;
            }
          });
        }
      }
    },
    /**
     * 判断教师/家长/学生可以点击班级空间，其他角色不行
     */
    isTrue(id) {
      let classId1 = [1034, 1035, 1036, 1037, 1038, 1039, 1040, 1100];
      let classId2 = [1013, 1014, 1015, 1016, 1017, 1019, 1020];

      if ([4, 5].includes(+this.$route.params.id) && classId2.includes(id)) {
        return false;
      }

      if (
        [4, 5].includes(+this.$route.params.id) &&
        [1045, 1046].includes(id)
      ) {
        return false;
      }

      if ([4, 5, 6].includes(+this.$route.params.id)) {
        return true;
      } else {
        if (classId1.includes(id)) {
          return false;
        } else {
          return true;
        }
      }
    },
    /**
     * 权限联动，权限表中名字相同的做同样操作
     * @param {Bollean} flag 选中状态
     * @param {Number} inx1 数组 下标
     * @param {Number} inx2 数组 下标
     * @param {Number} inx3 数组 下标
     * @modify 2019-5-13
     */
    setLinkage(flag, inx1, inx2, inx3) {
      if (inx1 !== 2) {
        // 后台权限不作联动
        let idObj = {
          subJect: [1012, 1054], // 学科圈组
          leave: [1107, 1100], // 请假
          teachingLive: [1108, 1015], // 教学直播
          classNotice: [1109, 1035], // 班级通知
          studyNotes: [1026, 1128], // 学习笔记
          learningFootprint: [1129, 1025], // 学习足迹
          classSchedule: [1040, 1131], // 班级课表
          classCircle: [1133, 1034], // 班级圈
          myAnswer: [1017, 1163], // 我的问答 -- 问答中心
          myQuestion: [1027, 1164], // 我的提问 -- 我的问答
        };

        if (flag) {
          let _id = this.nodeData[inx1].children[inx2].children[inx3].id;
          let _id2 = undefined;

          for (let key in idObj) {
            if (idObj[key].includes(_id)) {
              _id2 = idObj[key].filter((item) => item !== _id)[0];
            }
          }

          this.nodeData.forEach((item1, index1) => {
            item1.children.forEach((item2) => {
              let temp = false;

              item2.children.forEach((item3) => {
                if (
                  index1 !== 2 &&
                  item3.id === _id2 &&
                  this.isTrue(item3.id)
                ) {
                  item3.selected = true;
                  temp = true;

                  if (!this.nodeIds.includes(item3.id)) {
                    this.nodeIds.push(item3.id);
                  }
                }
              });

              if (temp) {
                item2.selected = true;
                item1.selected = true;
                temp = false;

                if (!this.nodeIds.includes(item2.id)) {
                  this.nodeIds.push(item2.id);
                }

                if (!this.nodeIds.includes(item1.id)) {
                  this.nodeIds.push(item1.id);
                }
              }
            });
          });
        } else {
          let _id = this.nodeData[inx1].children[inx2].children[inx3].id;
          let _id2 = undefined;

          for (let key in idObj) {
            if (idObj[key].includes(_id)) {
              _id2 = idObj[key].filter((item) => item !== _id)[0];
            }
          }

          this.nodeData.forEach((item1, index1) => {
            if (index1 !== 2) {
              item1.children.forEach((item2, index2) => {
                let temp = false;
                let arr = [];

                item2.children.forEach((item3, index3) => {
                  if (item3.id == _id2) {
                    if (
                      this.nodeIds.includes(item3.id) &&
                      !in_array(parseInt(item3.id), this.defaultNodeIds) &&
                      this.isTrue(item3.id)
                    ) {
                      this.nodeIds.splice(this.nodeIds.indexOf(item3.id), 1);
                      item3.selected = false;
                      temp = true;
                    }
                  } else {
                    arr.push(item3.id);
                  }
                });

                if (temp) {
                  let id_check = true;
                  temp = false;
                  arr = [...new Set(arr)];

                  arr.forEach((val) => {
                    if (this.nodeIds.includes(val)) {
                      if (id_check) {
                        id_check = false;
                      }
                    }
                  });

                  if (id_check) {
                    if (this.nodeIds.includes(item2.id)) {
                      this.nodeIds.splice(this.nodeIds.indexOf(item2.id), 1);
                      item2.selected = false;
                    }
                  }

                  let id_check1 = true;
                  if (this.nodeIds.includes(item1.id)) {
                    if (id_check1) {
                      id_check1 = false;
                    }
                  } else {
                    id_check1 = true;
                  }

                  if (id_check1) {
                    if (this.nodeIds.includes(item1.id)) {
                      this.nodeIds.splice(this.nodeIds.indexOf(item1.id), 1);
                      item1.selected = false;
                    }
                  }
                }
              });
            }
          });
        }
      }
    },
    /**
     * 非教师/学生/家长角色，不可点击班级空间
     * @param {Object} obj 二级菜单对象
     * @param {Object} obj1 三级菜单对象
     */
    setValue(obj, obj1 = {}) {
      
      // 所有角色--- 置灰已选：直播、教学资源、公开课、优课专辑、名师中心
      if([1048, 1049, 1050, 1051, 1052, 1176, 1177, 1178, 1179, 1183].includes(obj1.id)){
        return true
      }
      // 普通用户/自建角色 -- 置灰不可选 
      if (![2,3,4, 5, 6].includes(+this.$route.params.id) && [1012, 1020, 1054, 1056].includes(obj1.id)) {
        return true;
      }
      // 管理员隐藏资源审核、学生学分
      if ([2,3].includes(+this.$route.params.id) && [1019, 1020].includes(obj1.id)) {
        return true;
      }

      /******** app 应用 ********/
      // 教师、管理员特有权限：网络教研、学科圈组、协同备课、请假审批
      if (![2, 3, 6, 8].includes(+this.$route.params.id) && [1181, 1182, 1189].includes(obj1.id)) {
        return true;
      }

      // 家长、学生特有权限：成长档案
      if (![4, 5].includes(+this.$route.params.id) && 1186 === obj1.id) {
        return true;
      }

      // 教师、家长、学生特有权限：作业、作业分析、班级通知、班级课表、请假
      if (![4, 5, 6].includes(+this.$route.params.id) && [1184, 1185, 1187, 1188, 1190].includes(obj1.id)) {
        return true;
      }
      /******** app 应用 ********/


      if (+this.$route.params.id !== 6 && obj1.id === 1132) {
        return true;
      }

      if (![3, 6, 8].includes(+this.$route.params.id) && [1163].includes(obj1.id)) {
        return true;
      }

      // 只有教师可以点击学生档案
      if (+this.$route.params.id !== 6 && obj1.id === 1139) {
        return true;
      }

      // 只有学生可以点击学籍管理、智慧练习
      if (+this.$route.params.id !== 4 && [1150, 1151, 1154].includes(obj1.id)) {
        return true;
      }

      // 学生、家长不可以点击智慧组卷
      if ([4, 5].includes(+this.$route.params.id) && [1055, 1012, 1054, 1056, 1017].includes(obj1.id)) {
        return true;
      }

      if ([4, 5].includes(+this.$route.params.id) && (obj.id === 1013 || [1108, 1127, 1045, 1046].includes(obj1.id))) {
        return true;
      }

      if (+this.$route.params.id !== 4 && [1029, 1030].includes(obj1.id)) {
        return true;
      }

      // if (!is_empty(obj1) && obj1.id === 1020) {
      //   if ([4, 5, 6].includes(+this.$route.params.id)) {
      //     if (this.nodeIds.includes(1033)) {
      //       return false;
      //     } else {
      //       return true;
      //     }
      //   }

      //   return true;
      // }

      if (![4, 5, 6].includes(+this.$route.params.id) && (obj.id == 1033 || [1107, 1109, 1131, 1133].includes(obj1.id))) {
        return true;
      } else {
        return false;
      }

    },
    /**
     * 添加/删除 权限
     * @param {Boolean} selected 是否勾选
     * @param {Number} nodeid 当前id
     */
    setNodes(selected, nodeid) {
      if (selected) {
        if (!this.nodeIds.includes(nodeid)) {
          this.nodeIds.push(nodeid);
        }
      } else {
        if ( this.nodeIds.includes(nodeid) && !in_array(+nodeid, this.defaultNodeIds)) {
          this.nodeIds.splice(this.nodeIds.indexOf(nodeid), 1);
        }
      }
    },
    /**
     * 获取全部权限数据
     */
    getRoleData() {
      this.loading.status = true;
      this.loading.txt = "获取数据...";
      // 重写成功回调
      let success = (data) => {

        this.loadingTotalTime++;
        this.nodeData = data.data.role_node;
        
        // this.nodeData[1].children[0].children[14].selected = true
        console.log(JSON.parse(JSON.stringify(this.nodeData)), 10086)
      };
      let fail = (data) => {
        this.loading.status = false;
      };

      this.$ajax.get("api/role/role_data", {
        params: { id: this.$route.params.id },
        func: { success: success, fail: fail },
      });
    },
    /**
     * 获取选中的权限
     */
    getSelectNode() {
      let success = (data) => {
        this.loadingTotalTime++;
        this.nodeIds = deep_clone(data.data);
      };
      let fail = (data) => {
        this.loading.status = false;
      };

      this.$ajax.get("api/role/select_node_id", {
        params: { role_id: this.$route.params.id },
        func: { success: success, fail: fail },
      });
    },
    /**
     * 获取默认选中的权限
     */
    getDefaultSelectNode() {
      let success = (data) => {
        this.defaultNodeIds = data.data;
        this.loadingTotalTime++;
      };
      let fail = (data) => {
        this.loading.status = false;
      };

      this.$ajax.get("api/role/default_node_id", {
        params: {},
        func: { success: success, fail: fail },
      });
    },
    /**
     * 保存权限设置
     */
    submitRole() {
      this.loading.status = true;
      this.loading.txt = "保存数据...";

      let success = (data) => {
        this.loading.status = false;
        this.$message.success(data.msg);

        setTimeout(() => {
          this.$router.push("/user/user/platform-role");
        }, 1000);
      };
      let fail = (data) => {
        setTimeout(() => {
          this.subAbled = false;
        }, 3000);
        this.loading.status = false;
        this.$message.error(data.msg);
      };

      if (!is_empty(this.defaultNodeIds)) {
        this.defaultNodeIds.forEach((item, inx) => {
          if (!in_array(parseInt(item), this.nodeIds)) {
            this.nodeIds.push(item);
          }
        });
      }

      let param = {
        id: this.nodeIds,
        role_id: this.$route.params.id,
      };
      this.subAbled = true;

      this.$ajax.post("api/role/role_data", param, {
        func: { success: success, fail: fail },
      });
    },
    /**
     * 获取初始权限
     */
    resetRoleData() {
      this.loading.status = true;
      this.loading.txt = "重置数据...";

      let success = (res) => {
        this.$message.success(res.msg);
        this.loadingTotalTime = 2;
        this.getSelectNode();
        this.getRoleData();
      };
      let fail = (res) => {
        this.loading.status = false;
        this.$message.error(res.msg);
      };

      this.$ajax.post(
        "api/role/reset_role",
        { role_id: this.$route.params.id },
        { func: { success: success, fail: fail } }
      );
    },
    /**
     * 重置权限设置
     */
    reset_confirm() {
      let yes = (res) => {
        this.resetRoleData();
      };
      let cancel = (res) => {};

      this.e_confirm({
        yes: yes,
        cancel: cancel,
        txt: "确定重置该角色权限吗？",
      });
    },
    /**
     * 取消权限设置
     */
    cancel() {
      this.$router.go(-1);
    },
  },
};
</script>

<style lang="scss">
.zh-permission-settings {
  font-size: 14px;

  .activeTop {
    border-top: 1px solid #dddddd;
  }

  .el-col {
    border-bottom: 1px solid #dddddd;
  }

  .header-title {
    background-color: #dddddd;

    .el-col {
      padding: 8px 16px;
      text-align: center;
      line-height: 25px;
      color: #000000;
    }

    > div:first-child {
      border-left: 1px solid #dddddd;
    }

    > div:last-child {
      border-left: 1px solid #dddddd;
      border-right: 1px solid #dddddd;
    }
  }
  .content-setting {
    display: flex;

    .el-col {
      padding: 8px 16px;
      font-size: 16px;
      line-height: 25px;
    }

    > div:first-child {
      display: flex;
      align-items: center;
      padding-left: 50px;
      border-left: 1px solid #dddddd;
    }

    > div:last-child {
      padding-left: 50px;
      border-left: 1px solid #dddddd;
      border-right: 1px solid #dddddd;

      label {
        margin-left: 0;
        min-width: 112px;
      }
    }
  }

  .submit-button {
    padding-top: 50px;
  }
}
</style>